package creational_pattern.singleton.demo2;

import java.lang.reflect.Constructor;

// 测试反射打破单例模式
public class Client {
    public static void main(String[] args) throws Exception {
        // 获取单例对象的字节码对象
        Class<Singleton> clazz = Singleton.class;
        // 获取无参构造方法
        Constructor<Singleton> cons = clazz.getDeclaredConstructor();
        // 取消访问检查
        cons.setAccessible(true);
        // 创建Singleton对象
        Singleton instance = cons.newInstance();
        Singleton instance2 = cons.newInstance();

        System.out.println(instance == instance2);
    }
}
